package com.ys.controller.web.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.entity.sys.YsModalSelector;
import com.ys.entity.sys.YsModalSelectorField;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/sys/ysModalSelector")
public class WebYsModalSelectorController {
	
	/**
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model){

		return "sys/ysModalSelector/toAdd";
	}
	
	/**
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, Long id){
		
		YsModalSelector entity = new YsModalSelector().setId(id).queryFirst();
		String name = entity.getName();
		String nameTable = entity.getNameTable();
		String pathGridRequest = entity.getPathGridRequest();
		ToEditDto dto = new ToEditDto(id, name, nameTable, pathGridRequest);

		model.addAttribute("dto", dto);
		return "sys/ysModalSelector/toEdit";
	}
	@Data
	@AllArgsConstructor
	private static class ToEditDto{
		private Long id; // 
		private String name;
		private String nameTable; // 
		private String pathGridRequest;
	}
	
	/**
	 * 配置
	 */
	@GetMapping("/toSetting")
	public String toSetting(Model model, Long id){
		
		YsModalSelector entity = new YsModalSelector().setId(id).queryFirst();
		String name = entity.getName();
		String nameTable = entity.getNameTable();
		
		List<YsModalSelectorField> listYsModalSelectorField = new YsModalSelectorField().setPid(id).queryList();
		
		List<ToSettingDto.RowDto> rows = new ArrayList<ToSettingDto.RowDto>();
		for(YsModalSelectorField ysModalSelectorField : listYsModalSelectorField) {
			Long id2 = ysModalSelectorField.getId();
			String code = ysModalSelectorField.getCode();
			String name2 = ysModalSelectorField.getName();
			String type = ysModalSelectorField.getType();
			Integer stateCondition = ysModalSelectorField.getStateCondition();
			rows.add(new ToSettingDto.RowDto(id2, code, name2, type, stateCondition));
		}

		ToSettingDto dto = new ToSettingDto(id, name, nameTable, rows);
		model.addAttribute("dto", dto);
		return "sys/ysModalSelector/toSetting";
	}
	@Data
	@AllArgsConstructor
	private static class ToSettingDto{
		private Long id; // 
		private String name;
		private String nameTable; // 
		private List<RowDto> rows;
		
		@Data
		@AllArgsConstructor
		private static class RowDto{
			private Long id; // 
			private String code; // 
			private String name;
			private String type; // 
			private Integer stateCondition;
		}
	}
}
